package com.mach.platform.sprider.dev;

import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;
import org.joda.time.DateTime;

import java.io.IOException;

/*
    取得变频器数据抓取器,一个对象针对一个采集模块:电表
 */
public class TransducerDBDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;

    public TransducerDBDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }

    public static void main(String[] args) throws IOException {
        findAll(args);
    }
    public static void findAll(String[] args) throws IOException {
        int busNumStartIx = 1,busNumEndIx = 4; //总线地址开始与结束参数
        int tcpPortStartIx = 10001,tcpPortEndIx = 10001; //TCP端口开始与结束参数
        String ip = "192.168.1.1";
        for(int j = tcpPortStartIx; j <= tcpPortEndIx; j+=4){
            for(int i = busNumStartIx; i <= busNumEndIx; ++i){
                for(int k = 1; k <= 2; ++k){
                    readIdById(ip,j,i,"XXXX",k );
                }
            }
        }
    }

    public static void readIdById(String ip ,Integer port, Integer id,String devName, Integer addr) throws IOException {
        TransducerDBDev dev =  new TransducerDBDev(ip,port,id,devName);
        byte[] data = dev.readSth(addr, 2, 3);
        UU.printData(data);
    }

    public byte[] readSth(Integer addr, Integer num, Integer cmdCode){
        byte[] result = null;


        byte[] repData = new byte[0];
        try {
            repData = this .getDataRtu(addr,num);
//            if(repData.length <1)return null;
//            byte[] data = ByteUtil.subData(repData,3,4);
//            data = ByteUtil.reverse(data);
            result = repData;
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }



    @Override
    public void run(){

        long preSec = 0;
        long nowSec = 0;
        while(true){


            try {

//                long nowMill = System.currentTimeMillis();
//
//                nowSec = nowMill / 1000;
//
//                if(nowSec == preSec)continue;
//
//                Date now = new Date(nowMill);
//
//                int fyear = now.getYear() + 1900;
//                int fmonth = now.getMonth() + 1;
//                int fday = now.getDate();
//                int fhour = now.getHours();
//                int fmin = now.getMinutes();
//                int fsec = now.getSeconds();

                DateTime dateTime = new DateTime();
                int fyear = dateTime.getYear();
                int fmonth = dateTime.getMonthOfYear();
                int fday = dateTime.getDayOfMonth();
                int fhour = dateTime.getHourOfDay();
                int fmin = dateTime.getMinuteOfHour();
                int fsec = dateTime.getSecondOfMinute();

                Float volt =  this.getVolt();
                Float elect = this.getElect();
                Float power = this.getPower();
                Double enery = this.getEnery();

                try {
                    this.sqls.updateAsync("insert ignore into data_detail(dev_id, fyear, fmonth, fday, fhour, fmin, fsec,fdatetime, " +
                            "volt,elect,power,enery) value" +
                            "('"+this.devName+"',"+fyear+","+fmonth+","+fday+","+fhour+","+fmin+","+fsec+","+"date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s'),"
                            +volt+","+elect+","+power+","+enery+")");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    this.sqls.updateAsync("replace into data_cur (dev_id,volt,elect,power,update_time) value " +
                            "( " +
                            "'"+this.devName+"',"+
                            volt + ","+
                            elect + ","+
                            power + "," +
                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                                    "'%Y-%m-%d %H:%i:%s')" +
                            ")");
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                Thread.sleep(1000*60);


            } catch (Exception e) {
                e.printStackTrace();
            }

            preSec = nowSec;


        }


    }

    /*
      取变频器:电压
   */
    public Float getVolt() {
        Float result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40006,4);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,4);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getFloat(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    /*
     取变频器:电流
  */
    public Float getElect() {
        Float result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40022,4);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,4);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getFloat(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    /*
     取变频器:功率
  */
    public Float getPower() {
        Float result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40032,4);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,4);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getFloat(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

    /*
    取变频器:能量
 */
    public Double getEnery() {
        Double result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40266,8);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,8);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getDouble(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

}
